import os, sys, math, numpy

#Se agrega al path la carpeta ../lib
modulesdir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../lib'))
sys.path.insert(0, modulesdir)

#Se agrega al path la carpeta ../
modulesdir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))
sys.path.insert(0, modulesdir)

from lib.euclid import Vector3
from library import *

from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *

class Sphere(Library):
    def __init__(self, identifier, parameters):
        #Asociacion de Parametros
        name = parameters[0]
        self.posSphereX = parameters[1]
        self.posSphereY = parameters[2]
        self.posSphereZ= parameters[3]
        self.tamSphere = parameters[4] 
        
        self.spherePos= Vector3(self.posSphereX, self.posSphereY, self.posSphereZ)
        
        #Identificador
        self.identifier = identifier
        
        #Variable para que la Escena elimine el objeto de su lista de objetos, variable que indica la seleccion del objeto
        self.autoEliminate = False
        self.bSelected = False
        
        #Tipo, nombre y colores del objeto
        self.type = 'Sphere'
        self.name = name
        self.color = (1, 0, 0, 0)
        self.colorSelected = (0.5, 0.5, 0, 0)
        return 

    def getClassName():
        return 'Sphere'
    getClassName = staticmethod(getClassName)

    def getNameInMenu():
        return 'Esfera para prueba'
    getNameInMenu = staticmethod(getNameInMenu)

    def getParametersForCreate():
        title = 'Este es el titulo'
        parameters = [title, 
                      ('Nombre de la esfera', str), 
                      ('pos esfera x', float), 
                      ('pos esfera y', float), 
                      ('pos esfera z', float), 
                      ('Tamano Esfera', float),
                      ]
        return parameters
    getParametersForCreate = staticmethod(getParametersForCreate)

    def draw(self, selectMode=False, moving = True):
        '''Callback Display del objeto'''
        if(not selectMode):
            color = self.color
            if(self.bSelected):
                color = self.colorSelected
        else:
            inc = (1.0/255)
            red = (inc) * self.identifier
            color = (red, red, red,  0)
        
        radius = self.tamSphere
        slices = 100
        stacks = 100
        self.draw_sphere(self.spherePos, radius, slices, stacks, color)
        return 0

    def Mouse(self, x, y, camara, plano):
        '''CallBack Mouse'''
        print 'mouse sphere'
        return 0

    def Motion(self, x, y, width, heigth, camara, plano):
        '''CallBack Motion'''
        print 'motion phere'
        return 0

    def actionsToDo(self):
        '''Acciones del objeto'''
        self.actions = {}
        return self.actions.keys()

    def atributesToEdit(self):
        '''Atributos para editar'''
        self.atributes = {'Tamanio esfera': (self.getTamSphere(), lambda spinner1:self.setTamSphere(spinner1))}
        return self.atributes

    def getTamSphere(self):
        return self.tamSphere

    def setTamSphere(self, spinbutton_widget):
        valor = round(spinbutton_widget.get_value())
        self.tamSphere = valor
